package com.hlk.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.hlk.enums.UserStatusEnum;
import com.hlk.pojo.AppUser;
import com.hlk.user.mapper.AppUserMapper;
import com.hlk.user.service.AppUserMngService;
import com.hlk.utils.PageResult;
import com.hlk.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author hlk
 * @version 1.0
 * @date 2020-12-29 14:30
 */
@Service
public class AppUserMngServiceImpl implements AppUserMngService {
    @Autowired
    private AppUserMapper appUserMapper;

    @Override
    public PageResult selectAllUserList(String nickname, Integer status, Date startDate, Date endDate, Integer page, Integer pageSize) {
        Example example = new Example(AppUser.class);
        example.orderBy("createdTime").desc();
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(nickname)) {
            criteria.andLike("nickname", "%" + nickname + "%");
        }

        if (UserStatusEnum.isUserStatusValid(status)) {
            criteria.andEqualTo("activeStatus", status);
        }

        if (startDate != null) {
            criteria.andGreaterThanOrEqualTo("createdTime", startDate);
        }
        if (endDate != null) {
            criteria.andLessThanOrEqualTo("createdTime", endDate);
        }

        PageHelper.startPage(page, pageSize);
        List<?> list = appUserMapper.selectByExample(example);
        return PageUtils.getResult(page, list);
    }

    @Override
    public AppUser selectUserByUserId(String userId) {
        return appUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void freezeUserOrNot(String userId, Integer doStatus) {
        AppUser user = new AppUser();
        user.setId(userId);
        user.setActiveStatus(doStatus);
        appUserMapper.updateByPrimaryKeySelective(user);
    }
}
